<template>
  <el-dialog :visible.sync="showDialog" :title="title" :close-on-click-modal="false" @close="closeDialog">
    <el-form ref="form" label-width="200px" :model="queryData" :rules="rules">
      <el-form-item label="广告位名称" prop="advposname">
        <el-input v-model="queryData.advposname" style="width:400px" />
      </el-form-item>
      <el-form-item label="广告位描述" prop="advposdesc">
        <el-input v-model="queryData.advposdesc" style="width:400px" />
      </el-form-item>
      <el-form-item label="广告位尺寸" prop="advpossize">
        <el-input v-model="queryData.advpossize" style="width:400px" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addAdvPosApi, getAdvPosDetailApi, editAdvPosApi } from '@/api/adv/pos'
export default {
  name: '',
  data() {
    return {
      showDialog: false,
      // 请求参数
      queryData: {
        advposname: '',
        advposdesc: '',
        advpossize: ''
      },
      // 表单检验规则
      rules: {
        advposname: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        advposdesc: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        advpossize: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.queryData.id ? '修改广告位' : '新增广告位'
    }
  },
  methods: {
    // 关闭弹框
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    // 表单提交
    async btnOK() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      if (this.queryData.id) {
        await editAdvPosApi(this.queryData)
        this.$message.success('修改成功')
      } else {
        await addAdvPosApi(this.queryData)
        this.$message.success('添加成功')
      }
      this.closeDialog()
      this.$emit('updata')
    },
    // 广告位详情回显
    async getAdvPosDetail(id) {
      this.showDialog = true
      const { data: res } = await getAdvPosDetailApi(id)
      this.queryData = res
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
